/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuvalLib;

import arayuz.CepMuvakkitG;
import astroLib.APC_Time;
import astroLib.EarthPosition;
import astroLib.HijriCalendar;
import astroLib.MagDeclination;
import astroLib.MoonTimes;
import astroLib.Settings;
import java.util.Calendar;
import javax.microedition.lcdui.*;
import lang.LocalizationSupport;

/**
 * @author mgeden
 */
public class KerahatCanvas extends Canvas implements CommandListener {

    private int width,  height;
    private String israk,  duha,  isfirar,  istiva,  asrHanefiStr;
    private Font fontPI;
    private int fontSize;
    //private byte dayofMonth;
    private HijriCalendar hijriCalendar;
    private String  gregorianCalendar;
    private Settings preferences;
    private boolean isMilitary = true;
    private double[] kerahatTimes = new double[5];
    private Calendar now;
    private double MJD0h,MJD;
    private double[] field = new double[6];
    private  String[] varValue=new String[3];
    private double timeZone;
    private EarthPosition position;
    private MoonTimes moonRiseSet;
    public CepMuvakkitG midlet;
    private Command helpCommand,   backCommand;//holyDayCommand,

    /**
     * constructor
     */
    public KerahatCanvas() {

        try {
            // Set up this canvas to listen to command events
            setCommandListener(this);
            // Add the Exit commandastro

            //holyDayCommand = new Command(LocalizationSupport.getMessage("HOLYDAYS"), Command.HELP, 0);
            helpCommand= new Command(LocalizationSupport.getMessage("HELPELEMENT"), Command.HELP, 0);
            backCommand = new Command(LocalizationSupport.getMessage("BACKCOMMAND2"), Command.BACK, 0);
            addCommand(helpCommand);
           // addCommand(holyDayCommand);
            addCommand(backCommand);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void paint(Graphics g) {

        String timesStrings[] = {israk, duha, isfirar, asrHanefiStr, istiva};
        width = getWidth();
        height = getHeight();
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, width, height);
        fontPI = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, fontSize);
        g.setFont(fontPI);
        int fontHeight = fontPI.getHeight();
        int yPos, line = 0;
        g.setColor(0, 0, 139);
        String timesNames[] = {lang.LocalizationSupport.getMessage("ISRAK"), lang.LocalizationSupport.getMessage("DUHA"), LocalizationSupport.getMessage("ISTIVA"), LocalizationSupport.getMessage("ASRISANI"), LocalizationSupport.getMessage("ISFIRAR")};
        g.setColor(205, 0, 0);
        g.drawString(gregorianCalendar , width / 2, line++ * fontHeight, Graphics.HCENTER | Graphics.TOP);
         MJD=(int)(MJD0h+timeZone/24.0);
        hijriCalendar=getHijriCalendar(MJD);
        g.drawString(  hijriCalendar.getHicriTakvim(), width / 2, (line++) * fontHeight, Graphics.HCENTER | Graphics.TOP);
        g.setFont(fontPI);
        for (int i = 1; i <= 5; i++) {
            yPos = (++line) * fontHeight;
            g.setColor(65, 105, 225);
            if (i == 1 || i == 3||i==5) {
                g.setColor(135, 206, 250);
            }
            g.drawString(timesNames[i - 1], width / 7, yPos, Graphics.BASELINE | Graphics.LEFT);
            g.drawString(":", width / 2, yPos, Graphics.BASELINE | Graphics.HCENTER);
            g.setColor(65, 105, 225);
            if (i == 1 || i == 3||i==5) {
                g.setColor(135, 206, 250);
            }
            g.drawString(timesStrings[i - 1], 3 * width / 4, yPos, Graphics.BASELINE | Graphics.HCENTER);

        }

        String varName[] = {lang.LocalizationSupport.getMessage("MOONRISE"), LocalizationSupport.getMessage("MOONSET"),LocalizationSupport.getMessage("MAGVAR2")};


        yPos = (++line) * fontHeight;
        for (int i = 0; i <= 2; i++) {
            yPos = (++line) * fontHeight;
            g.setColor(255, 111, 0);
            if (i == 0 || i == 1) {
                g.setColor(0, 230, 0);
            }
            g.drawString(varName[i], width / 7, yPos, Graphics.BASELINE | Graphics.LEFT);
            g.drawString(":", width / 2, yPos, Graphics.BASELINE | Graphics.HCENTER);
            g.setColor(255, 215, 0);
            if (i == 0 || i == 1) {
                g.setColor(144, 238, 144);
            }
            g.drawString(varValue[i], 4 * width / 5, yPos, Graphics.BASELINE | Graphics.RIGHT);
        }
    }
  private HijriCalendar getHijriCalendar(double MJD) {

        hijriCalendar = new HijriCalendar(MJD);
        return hijriCalendar;

    }
    protected void keyPressed(int keyCode) {


        int pressAction = getGameAction(keyCode);
        //System.out.println(keyCode+"pressAction"+pressAction);
        if (pressAction == RIGHT || pressAction == UP || keyCode == 54) {
            MJD0h++;
          gregorianCalendar=APC_Time.DateTime(MJD0h+timeZone/24.0);
            Settings.getKerahatTimes(MJD0h, kerahatTimes);
            setTimesStr(kerahatTimes);
            setInformation (MJD0h);
        //System.out.println("I was thhere "+number);
        } else if (pressAction ==FIRE){
                this.MJD0h = getMJD0h();
                Settings.getKerahatTimes(MJD0h, kerahatTimes);
               gregorianCalendar=APC_Time.DateTime(MJD0h+timeZone/24.0);
                setTimesStr(kerahatTimes);
                setInformation (MJD0h);
                }
        else if (pressAction == LEFT || pressAction == DOWN) {
            //now=APC_Time.CalDat(MJD0h--);
            //setGregorianCalendar(now);
            MJD0h--;
            //System.out.println(MJD0h);
        // modifiedJulianDay=APC_Time.CalDat(MJD0h+timeZone/24.0);
        // setGregorianCalendar( modifiedJulianDay);
        // Settings.getSalatTimes(MJD0h, fiveTimes);
        //setTimesStr(fiveTimes);
        //System.out.println(number);
           gregorianCalendar=APC_Time.DateTime(MJD0h+timeZone/24.0);

            Settings.getKerahatTimes(MJD0h, kerahatTimes);
            setTimesStr(kerahatTimes);
            setInformation (MJD0h);
        }
        // make sure to repaint
        this.repaint();
    }

    private void setTimesStr(double[] kerahatTimes) {
        this.isMilitary = Settings.optionPref.getIsMilitary();
        this.israk = APC_Time.Time(kerahatTimes[0], isMilitary);
        this.duha = APC_Time.Time(kerahatTimes[1], isMilitary);
        this.isfirar = APC_Time.Time(kerahatTimes[2], isMilitary);
        this.istiva = APC_Time.Time(kerahatTimes[3], isMilitary);
        this.asrHanefiStr = APC_Time.Time(kerahatTimes[4], isMilitary);
    }

    public void setPreferences(Settings preferences) {
        this.preferences = preferences;
        this.MJD0h = getMJD0h();
        Settings.getKerahatTimes(MJD0h, kerahatTimes);
        setTimesStr(kerahatTimes);
        this.position = new EarthPosition(Settings.locationPrefer.getLatitude(), Settings.locationPrefer.getLongitude());
        this.setFullScreenMode(Settings.optionPref.getIsFullScreen());
        this.isMilitary = Settings.optionPref.getIsMilitary();
        setFontSize(Settings.optionPref.getFontSize());
        setInformation (MJD0h);
       gregorianCalendar=APC_Time.DateTime(MJD0h+timeZone/24.0);



    }
        private void setInformation (double MJD0h)
    {
            this.moonRiseSet=getMoonRiseSet(MJD0h);
            this.varValue[0]=APC_Time.Time(moonRiseSet.getMoonRiseTime(),Settings.optionPref.getIsMilitary());
            this.varValue[1]=APC_Time.Time(moonRiseSet.getMoonSetTime(),Settings.optionPref.getIsMilitary());
            this.varValue[2]=APC_Time.formatDoubleValue(getMagneticDeclination(MJD0h)) + "º";
    }

    private void setFontSize(int fontSize) {
        switch (fontSize) {
            case 0:
                this.fontSize = 8;
                break;
            case 1:
                this.fontSize = 0;
                break;
            case 2:
                this.fontSize = 16;
                break;
        }
    }

    private double getMJD0h() {
        now = Calendar.getInstance();
        if (Settings.locationPrefer.getTimeZoneSrc() != 0) {
            timeZone = Settings.locationPrefer.getTimeZone();
            if (Settings.locationPrefer.getIsDSTOn()) {
                timeZone++;
            }
        } else {
            timeZone = now.getTimeZone().getRawOffset() / (60 * 60 * 1000);
        }

        return APC_Time.Mjd(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1, now.get(Calendar.DAY_OF_MONTH), 0, 0, 0) - timeZone / 24.0;
    }

 /*   private HijriCalendar getHijriCalendar(double MJD) {
       
        hijriCalendar = new HijriCalendar(MJD);
        return hijriCalendar;

    }*/

    private double getMagneticDeclination(double MJD0h) {
        return MagDeclination.SGMagVar(position, MJD0h, Settings.locationPrefer.getAltitude(), field);
    }

  

/*    private EarthHeading getQiblaInfo() {
        EarthPosition qibla = new EarthPosition(21.416666667, 39.816666);
        return (position.toEarthHeading(qibla));
    }*/

    private MoonTimes getMoonRiseSet(double MJD0h) {

        moonRiseSet = new MoonTimes(position, MJD0h);
        return moonRiseSet;
    }

    public void commandAction(Command arg0, Displayable arg1) {

        if (arg0 == backCommand) {
            Display.getDisplay(midlet).setCurrent(midlet.getSecenekler());
       /* } else if (arg0 == holyDayCommand) {
            //System.out.println("I was there");
         //   display.setCurrentItem(item);
            //Display.getDisplay(midlet).setCurrent(midlet.getHolyDayTxt());*/

        } else if (arg0 == helpCommand) {
            //System.out.println("I was there");
         //   display.setCurrentItem(item);

            Display.getDisplay(midlet).setCurrent(midlet.getTextBoxKerahat());

        }

        repaint();
    }
}